using DealerPlatform.Domain;
using DealerPlatform.Service.ProductApp;
using DealerPlatform.Service.ShoppingCartApp;
using DealerPlatform.Service.ShoppingCartApp.Dtos;
using DealerPlatform.Web.Filters;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DealerPlatform.Web.Controllers;

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[CtmAuthorizationFilter]
public class ShoppingCartController(
    IShoppingCartAppService shoppingCartAppService,
    IProductService productService)
    : BaseController
{
    [HttpPost]
    public async Task<ShoppingCart> SetShoppingCart(ShoppingCartInputDto input)
    {
        var customerNo = HttpContext.Items["CustomerNo"]?.ToString();
        input.CustomerNo = customerNo;
        return await shoppingCartAppService.SetShoppingCart(input);
    }

    [HttpGet("num")]
    public async Task<int> GetShoppingCartNum()
    {
        var customerNo = HttpContext.Items["CustomerNo"]?.ToString();
        return await shoppingCartAppService.GetShoppingCartNum(customerNo);
    }

    [HttpGet]
    public async Task<dynamic> GetShoppingCartDtoAsync()
    {
        var customerNo = HttpContext.Items["CustomerNo"]?.ToString();
        var carts = await shoppingCartAppService.GetShoppingCartDtos(customerNo);
        var productDtos = carts.Select(m => m.ProductDto);
        var types = productDtos?.Select(
            m => new { m?.TypeNo, m?.TypeName, TypeSelect = false }).Distinct();
        return new { carts, types };
    }

    [HttpPost("CartSelected")]
    public async Task<string> UpdateCartSelected(ShoppingCartSelectedEditDto edit)
    {
        var customerNo = HttpContext.Items["CustomerNo"]?.ToString();
        return await shoppingCartAppService.UpdateCartSelected(edit,customerNo);
    }
}